<template>
  <div>
    <div class="toolbar">
      <el-form :inline="true" :model="q" size="mini">
        <el-form-item label="问题类型：">
          <el-select v-model="q.type_id" clearable placeholder="请选择">
            <el-option v-for="item in problemsTypeList" :key="item.id" :label="item.desc" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="query">查询</el-button>
          <el-button type="danger" @click="reload">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-row type="flex" class="row-bg btn-row" justify="end">
      <el-button size="mini" @click="add" type="primary" icon="el-icon-plus">新增问题类型
      </el-button>
    </el-row>
    <el-table class="el-calendar-table" :data="listData.items" v-loading.body="listLoading" size="mini" stripe border
      fit highlight-current-row>
      <el-table-column prop="id" label="ID" width="80">
      </el-table-column>
      <el-table-column prop="type.desc" label="问题类型" width="100">
      </el-table-column>
      <el-table-column prop="title" label="问题文本" width="260">
        <template slot-scope="scope"><div class="ellipsis">{{scope.row.title}}</div></template>
      </el-table-column>
      <el-table-column prop="answer" label="答案文本" min-width="260">
        <template slot-scope="scope"><div class="ellipsis">{{scope.row.answer}}</div></template>
      </el-table-column>
      <el-table-column width="160" prop="created_at" label="创建时间">
      </el-table-column>
      <el-table-column width="160" prop="updated_at" label="更新时间">
      </el-table-column>
      <el-table-column label="操作" width="130" fixed="right">
        <template slot-scope="scope">
          <div>
            <el-button type="primary" size="mini" @click="update(scope.row)">编辑</el-button>
            <el-button type="success" size="mini" @click="deleteRow(scope.row)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="q.page_no"
        :page-sizes="$store.state.app.pageSizeArr" :page-size="q.page_size"
        layout="total, sizes, prev, pager, next, jumper" :total="listData.total">
      </el-pagination>
    </div>
    <save-update v-if="showUpdate" :showUpdate.sync="showUpdate" :id="show_id" :problemsTypeList="problemsTypeList">
    </save-update>
  </div>
</template>

<script>
import { getCommonProblemsList, getProblemsList, deleteCommonProblemById } from '@/api'
import saveUpdate from './saveUpdate.vue'
import { queryFilter } from "@/utils/patch";
export default {
  name: "index",
  components: { saveUpdate },
  data () {
    return {
      show_id: null,
      listData: {},
      q: { page_size: 30, page_no: 1, pro_id: this.$store.state.user.userinfo.pro_id },
      listLoading: false,
      showUpdate: false,
      problemsTypeList: []
    }
  },
  created () {
    getProblemsList().then(res => {
      if (res.status === 0) {
        this.problemsTypeList = res.data.items
      }
    })
    this.getList()
  },
  methods: {
    add () {
      this.show_id = null
      this.showUpdate = true
    },
    update (row) {
      this.show_id = row.id
      this.showUpdate = true
    },
    deleteRow (row) {
      this.$confirm(`确定删除【${row.id}】这条数据吗？`, '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCommonProblemById(row.id).then(res => {
          if (res.status === 0) {
            this.$message({ type: 'success', message: '删除成功' })
            this.getList()
          }
        })
      }).catch(() => {
        this.$message({
          message: '已取消！',
          type: 'info'
        })
      })

    },
    query () {
      this.q = Object.assign(this.q, { page_no: 1, page_size: 30 })
      this.getList()
    },
    reload () {
      this.getList({ page_no: 1, page_size: 30 })
    },
    handleSizeChange (val) {
      this.q.page_size = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.q.page_no = val
      this.getList()
    },

    getList () {
      let queryObj = queryFilter(this.q)
      getCommonProblemsList(queryObj).then(res => {
        if (res.status === 0) {
          this.listLoading = false
          this.listData = res.data
        }
      })
    }
  },
  watch: {
    showUpdate () {
      if (!this.showUpdate) {
        this.getList()
      }
    }
  }
}
</script>

<style scoped>
</style>
